package com._520it.crm.service.impl;

import com._520it.crm.domain.AuditInsurance;
import com._520it.crm.domain.InsuranceInfoChange;
import com._520it.crm.mapper.AuditInsuranceMapper;
import com._520it.crm.mapper.InsuranceInfoChangeMapper;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.AuditInsuranceQueryObject;
import com._520it.crm.service.IAuditInsuranceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class AuditInsuranceServiceImpl implements IAuditInsuranceService {

    @Autowired
    private AuditInsuranceMapper auditInsuranceMapper;
    @Autowired
    private InsuranceInfoChangeMapper insuranceInfoChangeMapper;




    @Override
    public int insert(AuditInsurance record) {
        return auditInsuranceMapper.insert(record);
    }

    @Override
    public int updateByPrimaryKey(AuditInsurance record) {
        return auditInsuranceMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageResult query(AuditInsuranceQueryObject qo) {
        Integer count = auditInsuranceMapper.queryForCount(qo);
        if (count == 0){
            return new PageResult(0, Collections.EMPTY_LIST);
        }

        //查询所有批改表中有审核时间的数据
        List<InsuranceInfoChange> IicDates = insuranceInfoChangeMapper.queryForRows();
        //去重操作
        //查询出待审核表中的所有已批改单号
        List<String> orderBillNumberList = auditInsuranceMapper.queryOrderBillNumber();
        //循环遍历出所有已批改的单号,判断审核表中有没有包含单号,如果没有就保存到审核表中
        for (InsuranceInfoChange IicDate : IicDates) {
            if(!orderBillNumberList.contains(IicDate.getOrderbill().getOrderBillNumber())){
                AuditInsurance newAi = new AuditInsurance();
                newAi.setInsuranceInfoChange(IicDate);
                newAi.setEmp(IicDate.getEmployee());
                auditInsuranceMapper.insert(newAi);
            }
        }

        List<AuditInsurance> rowList = auditInsuranceMapper.queryForList(qo);

        return new PageResult(count,rowList);
    }
}
